package edu.cuit.zhuyimeng.framework.common.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;

@Configuration
@EnableAsync
@Slf4j
public class ExecutorAutoConfiguration {

    private final int coreSize = 10;

    private final int maxSize = 25;

    //单位: s
    private final int keepAlive = 5;

    private final int arrayBlockQueueSize = 40;

    @Bean
    @Primary
    public Executor executor() {
        log.info("Executor 开始配置...");
        ThreadPoolTaskExecutor threadPoolExecutor = new ThreadPoolTaskExecutor();
        threadPoolExecutor.setCorePoolSize(coreSize);
        threadPoolExecutor.setMaxPoolSize(maxSize);
        threadPoolExecutor.setKeepAliveSeconds(keepAlive);
        threadPoolExecutor.setQueueCapacity(arrayBlockQueueSize);
        threadPoolExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.AbortPolicy());
        threadPoolExecutor.initialize();
        log.info("Executor 加载完毕");
        return threadPoolExecutor;
    }

}
